<template>
	<view>
		<tui-sticky :scrollTop="scrollTop" stickyHeight="51px" container>
			<view class="padding bg-white" slot="header" ref="sticky">
				<u-search @search="list" placeholder="权限名称查询" v-model="pageRequest.condition.authName" action-text="添加" @custom="navTo('edit')"></u-search>
			</view>
			<template slot="content">
				<view class="margin-top">
					<view class="solid-bottom" v-for="(item, index) in pageData.content" :key="item.id">
						<tui-swipe-action :showMask="false" :actions="actions" @click="edit($event, item)">
							<template v-slot:content>
								<u-cell-item :title-style="{ width: '100%' }" icon="lock" :title="item.authName" :arrow="true" arrow-direction="right">
									<view slot="label" class="grid col-2">
										<view>编号：{{ item.authCode}}</view>
										<view>类型：{{ item.authTypeName}}</view>
										<view>序号：{{ item.sequence || 0 }}</view>
									</view>
								</u-cell-item>
							</template>
						</tui-swipe-action>
					</view>
					<u-loadmore :status="pageData.page < pageData.pages ? 'loading' : 'nomore'" bg-color="" class="padding" />
				</view>
			</template>
		</tui-sticky>
	</view>
</template>

<script>
export default {
	onLoad: function(option) {
		this.list(true);
	},
	onPullDownRefresh:function(){
		this.list(true);
	},
	onReachBottom: function() {
		this.list();
	},
	//页面滚动执行方式
	onPageScroll: function(e) {
		this.scrollTop = e.scrollTop;
	},
	data() {
		return {
			scrollTop: 0, // 滚动高度
			orgId: '', // 组织ID
			actions: [
				{
					name: '修改',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#ffaa00'
				},
				{
					name: '删除',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#ff0000'
				},
				{
					name: '分配',
					color: '#fff',
					fontsize: 30, //单位rpx
					width: 70, //单位px
					background: '#5555ff'
				}
			],
			pageRequest: {
				page: 1,
				rows: 15,
				sort: 'sequence:asc',
				condition: {}
			},
			pageData: {
				page: 1,
				pages: 1,
				content: []
			}
		};
	},
	methods: {
		list: function(first) {
			if (first) {
				this.pageRequest.page = 1;
			} else {
				if (this.pageData.page < this.pageData.pages) {
					this.pageRequest.page = this.pageRequest.page + 1;
				} else {
					return;
				}
			}
			this.$request.post(
				'/admin/sys/auth/list',
				this.pageRequest,
				res => {
					let pageData = res.data || {};
					if (this.pageRequest.page > 1) {
						pageData.content = (this.pageData.content || []).concat(pageData.content);
					}
					this.pageData = pageData;
				},
				false,
				false,
				true
			);
		},
		edit: function(e, item) {
			if (e.index == 0) {
				uni.navigateTo({
					url: `edit?info=${encodeURIComponent(JSON.stringify(item))}`
				});
			} else if (e.index == 1) {
				this.$utils.showModal(
					'确认删除？',
					() => {
						this.$request.post(
							'/admin/sys/auth/deleteById',
							item,
							res => {
								this.list(true);
							},
							true,
							true,
							true
						);
					},
					true
				);
			} else if (e.index == 2) {
				let url = '';
				if(item.authTypeCode == 'MENU') {
					url = `menu-list?info=${encodeURIComponent(JSON.stringify(item))}`
				} else if (item.authTypeCode == 'ELEMENT') {
					url = `elem-list?info=${encodeURIComponent(JSON.stringify(item))}`
				} else if (item.authTypeCode == 'RESOURCE') {
					url = `res-list?info=${encodeURIComponent(JSON.stringify(item))}`
				}
				if (url) {
					uni.navigateTo({
						url: url
					});
				}
			}
		},
		navTo: function(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
